//
//  BiOrderTimePicker.m
//  GottaWash
//
//  Created by bindu on 14-9-3.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiOrderTimePicker.h"

#define kDuration 0.3

@interface BiOrderTimePicker()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIPickerView * _addressPicker;
    NSDateFormatter * _dateFormater;
}
@property (nonatomic, copy) NSString * date1Str;
@property (nonatomic, copy) NSString * date2Str;
@property (nonatomic, retain) NSMutableArray * data1Array;
@property (nonatomic, retain) NSMutableArray * data2Array;
@end

@implementation BiOrderTimePicker
@synthesize date1Str;
@synthesize date2Str;
@synthesize data1Array;
@synthesize data2Array;

- (id)init
{
    self = [super initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 216+44)];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        
        // Initialization code
        UIView * topBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 44)];
        topBgView.backgroundColor = [UIColor grayColor];
        [self addSubview:topBgView];
        [topBgView release];
        
        UIButton * cancelBtn = GET_BUTTON(CGRectMake(0, 1, 60, 42), 15, NO, [UIColor whiteColor]);
        [cancelBtn addTarget:self action:@selector(onCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self addSubview:cancelBtn];
        
        UILabel * tipLabel = GET_LABEL(CGRectMake(0, (44-13)/2, MAIN_SCREEN_WIDTH, 13), 13, NO, [UIColor whiteColor], NSTextAlignmentCenter);
        tipLabel.text = @"请选择取件时间";
        [topBgView addSubview:tipLabel];
        
        UIButton * completeBtn = GET_BUTTON(CGRectMake(MAIN_SCREEN_WIDTH-60, 1, 60, 42), 15, NO, [UIColor whiteColor]);
        [completeBtn addTarget:self action:@selector(onCompleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self addSubview:completeBtn];
        
        _addressPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, MAIN_SCREEN_WIDTH, 216)];
        _addressPicker.delegate = self;
        _addressPicker.dataSource = self;
        [self addSubview:_addressPicker];
        [_addressPicker release];
        
		_dateFormater = [[NSDateFormatter alloc] init];
		[_dateFormater setDateFormat:@"yyyy-MM-dd"];
        
        [self refreshPickViewData];
    }
    
    return self;
}

- (void)showInView:(UIView *) view
{
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:@"DDAddressPickerView"];
    
    self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    
    [view addSubview:self];
}

- (void)dealloc
{
    self.data1Array = nil;
    self.data2Array = nil;
    [_dateFormater release];
    
    [super dealloc];
}

- (NSString *)getSelectTime
{
    return [NSString stringWithFormat:@"%@ %@", self.date1Str, self.date2Str];
}

- (int)getCurHour
{
    NSDate* now = [NSDate date];
    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
    NSInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:now];
    return [comps hour];
}

- (void)refreshPickViewData
{
    int hour = [self getCurHour];
    
    if (hour > 20)
    {
        self.data1Array = [NSMutableArray arrayWithObjects:@"明天", @"后天", nil];
        self.data2Array = [NSMutableArray arrayWithObjects:@"10:00-12:00", @"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
        NSDate *date = [[[NSDate alloc] initWithTimeIntervalSinceNow:24*60*60] autorelease];  // 明天
        self.date1Str = [_dateFormater stringFromDate:date];
        self.date2Str = @"10:00-12:00";
    }
    else
    {
        self.data1Array = [NSMutableArray arrayWithObjects:@"今天", @"明天", @"后天", nil];
        self.date1Str = [_dateFormater stringFromDate:[NSDate date]];
        
        if (hour < 10)
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"10:00-12:00", @"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            self.date2Str = @"10:00-12:00";
        }
        else if (hour < 12)
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            self.date2Str = @"12:00-14:00";
        }
        else if (hour < 14)
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            self.date2Str = @"14:00-16:00";
        }
        else if (hour < 16)
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            self.date2Str = @"16:00-18:00";
        }
        else if (hour < 18)
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"18:00-20:00", @"20:00-22:00", nil];
            self.date2Str = @"18:00-20:00";
        }
        else
        {
            self.data2Array = [NSMutableArray arrayWithObjects:@"20:00-22:00", nil];
            self.date2Str = @"20:00-22:00";
        }
    }
}

#pragma mark - PickerView lifecycle
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return self.data1Array.count;
            break;
        case 1:
            return self.data2Array.count;
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            return [self.data1Array objectAtIndex:row];
        }
            break;
        case 1:
        {
            return [self.data2Array objectAtIndex:row];
        }
            break;
        default:
            return nil;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component)
    {
        case 0:
        {
            NSTimeInterval timer = 24 * 60 * 60;
            if (0 == row)
            { // 今天
                self.date1Str = [_dateFormater stringFromDate:[NSDate date]];
                
                int hour = [self getCurHour];
                if (hour < 10)
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"10:00-12:00", @"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
                    self.date2Str = @"10:00-12:00";
                }
                else if (hour < 12)
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
                    self.date2Str = @"12:00-14:00";
                }
                else if (hour < 14)
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
                    self.date2Str = @"14:00-16:00";
                }
                else if (hour < 16)
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
                    self.date2Str = @"16:00-18:00";
                }
                else if (hour < 18)
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"18:00-20:00", @"20:00-22:00", nil];
                    self.date2Str = @"18:00-20:00";
                }
                else
                {
                    self.data2Array = [NSMutableArray arrayWithObjects:@"20:00-22:00", nil];
                    self.date2Str = @"20:00-22:00";
                }
            }
            else if (1 == row)
            { // 明天
                NSDate *date = [[[NSDate alloc] initWithTimeIntervalSinceNow:timer] autorelease];
                self.date1Str = [_dateFormater stringFromDate:date];
                self.data2Array = [NSMutableArray arrayWithObjects:@"10:00-12:00", @"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            }
            else if (2 == row)
            { // 后天
                NSDate *date = [[[NSDate alloc] initWithTimeIntervalSinceNow:2*timer] autorelease];
                self.date1Str = [_dateFormater stringFromDate:date];
                self.data2Array = [NSMutableArray arrayWithObjects:@"10:00-12:00", @"12:00-14:00", @"14:00-16:00", @"16:00-18:00", @"18:00-20:00", @"20:00-22:00", nil];
            }
            
            [_addressPicker selectRow:0 inComponent:1 animated:NO];
            [_addressPicker reloadComponent:1];
        }
            break;
        case 1:
        {
            self.date2Str = [self.data2Array objectAtIndex:row];
        }
            break;
        default:
            break;
    }
}

#pragma mark - Button lifecycle
- (void)onCancelBtnClick:(id)sender
{
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"BiTimerPickerView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate)
    {
        [self.delegate actionSheet:self clickedButtonAtIndex:0];
    }
}

- (void)onCompleteBtnClick:(id)sender
{
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"BiTimerPickerView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if(self.delegate)
    {
        [self.delegate actionSheet:self clickedButtonAtIndex:1];
    }
}
@end
